package cn.hpclub.server.controller.admin;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

import com.jfinal.aop.Before;
import com.jfinal.core.JFinal;
import com.jfinal.upload.UploadFile;

import cn.hpclub.server.bean.SystemConfig;
import cn.hpclub.server.interceptor.AdminInterceptor;
import cn.hpclub.server.model.Themes;
import cn.hpclub.server.util.CommonUtil;


/**
 * 后台类 - UI控件模板
 * 
 */
@Before(AdminInterceptor.class)
public class TemplateController extends BaseAdminController<Themes> {

	private File upload;// 上传文件
	private String uploadFileName;// 上传文件名
	
	// 列表
	public void index() {
		findByPage();
		render("/admin/template_list_page.tpl");
	}
	// 列表
	public void list() {
		findByPageOrderBy("sort");
        setAttr("keyword", this.getKeyword());
		render("/admin/template_list_page.tpl");
	}
	// 添加
	public void add() {
		String sid = getSession().getId();
		setAttr("sid",sid);
		render("/admin/template_add_page.tpl");
	}
	
	//ueditor图片上传
	public void uploadimage() {
		render("/jsp/controller.jsp");
	}
	
	//uploadify文件上传
	public void uploadify(){
		UploadFile imgageFile = getFile();	
		if (imgageFile != null) {
			upload = imgageFile.getFile();
			uploadFileName = imgageFile.getFileName();
		}		
		if (upload == null) {
			ajaxJsonErrorMessage("请选择上传文件!");
			return;
		}
		String allowedUploadImageExtension = getSystemConfig().getAllowedUploadImageExtension().toLowerCase();
		if (StringUtils.isEmpty(allowedUploadImageExtension)){
			ajaxJsonErrorMessage("不允许上传图片文件!");
			return;
		}
		String imageExtension =  StringUtils.substringAfterLast(uploadFileName, ".").toLowerCase();
		String[] imageExtensionArray = allowedUploadImageExtension.split(SystemConfig.EXTENSION_SEPARATOR);
		if (!ArrayUtils.contains(imageExtensionArray, imageExtension)) {
			ajaxJsonErrorMessage("只允许上传图片文件类型: " + allowedUploadImageExtension + "!");
			return;
		}
		int uploadLimit = getSystemConfig().getUploadLimit() * 1024;
		if (uploadLimit != 0) {
			if (upload != null && upload.length() > uploadLimit) {
				ajaxJsonErrorMessage("文件大小超出限制!");
				return;
			}
		}
		File uploadImageDir = new File(JFinal.me().getServletContext().getRealPath(SystemConfig.UPLOAD_IMAGE_DIR));
		if (!uploadImageDir.exists()) {
			uploadImageDir.mkdirs();
		}
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMM");
		String dateString = simpleDateFormat.format(new Date());
		String uploadImagePath = SystemConfig.UPLOAD_IMAGE_DIR + dateString + "/" + CommonUtil.getUUID() + "." + imageExtension;
		File file = new File(JFinal.me().getServletContext().getRealPath(uploadImagePath));
		try {
			FileUtils.copyFile(upload, file);
		} catch (IOException e) {
			e.printStackTrace();
		}
		Map<String, String> jsonMap = new HashMap<String, String>();
		jsonMap.put(STATUS, SUCCESS);
		jsonMap.put("url", JFinal.me().getServletContext().getContextPath() + uploadImagePath);
		renderJson(jsonMap);
	}
}
